﻿using System;
using System.Windows.Forms;
using TestsModel;
using TestsModel.Data;
using TestsModel.Enums;

namespace TestContentTool.TestControls
{
    public partial class CheckTestControl : UserControl, IIdentifiableTestControl
    {
        private readonly string id = Guid.NewGuid().ToString();
        public string Id { get { return id; }}
        public TestTypes TestType { get { return TestTypes.CheckTest; } }

        public string Display
        {
            get
            {
                return checkTestQestionTextBox.Text;
            }
        }

        private CheckTest checkTest;

        public CheckTestControl()
        {
            InitializeComponent();
            checkTest = new CheckTest();
        }
        public CheckTestControl(CheckTest test)
        {
            InitializeComponent();
            if (test == null)
                throw new ArgumentNullException();

            checkTest = test;
            Initialize();
        }

        private void Initialize()
        {
            checkTestQestionTextBox.Text = checkTest.QuestionText;
        }

        private void checkTestAddButton_Click(object sender, EventArgs e)
        {
            var textDialog = new TextEditDialog() { Value = string.Empty };
            if (textDialog.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(textDialog.Value))
            {
                checkedListBox1.Items.Add(textDialog.Value, false);
            }
        }

        private void checkTestEditButton_Click(object sender, EventArgs e)
        {
            var textDialog = new TextEditDialog()
            {
                Value = (string)checkedListBox1.SelectedItem
            };
            if (textDialog.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(textDialog.Value))
            {
                checkedListBox1.SelectedItem = textDialog.Value;
            }
        }

        private void checkTestRemoveButton_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Remove(checkedListBox1.SelectedItem);
        }

        public CheckTest GetTest()
        {
            checkTest.QuestionText = checkTestQestionTextBox.Text;
            checkTest.Statements.Clear();
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkTest.Statements.Add(new Pair<string, bool>((string)checkedListBox1.Items[i],
                    checkedListBox1.CheckedIndices.Contains(i)));
            }

            return checkTest;
        }
    }
}
